package com.shop.user.atom.atomsvc.impl;

import com.shop.common.entity.user.UserBase;
import com.shop.common.entity.user.UserPay;
import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.tmpl.BaseService;
import com.shop.common.util.state.UserState;
import com.shop.user.atom.atomsvc.IUserAtomSvc;
import com.shop.user.atom.dao.UserBaseMapper;
import com.shop.user.conf.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserAtomSvc extends BaseService implements IUserAtomSvc {

    final static Logger logger = LoggerFactory.getLogger(UserAtomSvc.class);

    @Autowired
    UserBaseMapper userBaseMapper;
    @Autowired
    UserPayAtomSvc userPayService;


    @Override
    public ServiceBean<String> addUser() {
        return null;
    }

    @Override
    public ServiceBean<Integer> addUser(UserBase userBase) {
        userBase.setStatus(UserState.INIT.value());//账号状态
        userBase.setCreateTime(DateUtil.newNowDate());//注册日期
        int i = userBaseMapper.insertSelective(userBase);
        Integer userId = userBase.getUserId();
        logger.info("【用户注册】注册成功,用户账号为:" + userId);
        userBase.setNickname("用户" + userId);
        userBaseMapper.updateByPrimaryKeySelective(userBase);
        System.out.println(userId);
        if (!opSuccess(i)) {
            logger.info("【用户注册】注册失败");
            return resultFailed("注册失败");
        }
        UserPay userPay = new UserPay();
        userPay.setUserId(userId);
        ServiceBean serviceBean = userPayService.insertUserPay(new InputBean(userPay));
        serviceBean.setData(userId);
        return serviceBean;
    }

    @Override
    public Boolean accountUse(String account) {
        UserBase userBase = userBaseMapper.selectBaseUserByAccountMapping(account);
        return userBase != null;
    }

    @Override
    public ServiceBean<UserBase> selectBaseUser(UserBase userBase) {
        userBase = userBaseMapper.selectByPrimaryKey(userBase.getUserId());
        if (userBase == null) {
            resultFailed("用户不存在");
        }
        return resultModel(true, "", userBase);
    }

    @Override
    public UserBase selectBaseUserByAccountMapping(String account) {
        return userBaseMapper.selectBaseUserByAccountMapping(account);
    }

    @Override
    public ServiceBean updateBaseUser(UserBase userBase) {
        int i = userBaseMapper.updateByPrimaryKeySelective(userBase);
        logger.info("【用户修改信息】用户id为：" + userBase.getUserId());
        return resultMsg(opSuccess(i), "");
    }

    @Override
    public UserBase selectBaseUserById(Integer uid) {
        UserBase user = userBaseMapper.selectByPrimaryKey(uid);
        return user;
    }

}
